<template>
  <a-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form" layout="inline">
        <a-row :gutter="24">
          <a-col :md="6" :sm="24">
            <a-form-item
              label="姓名">
                <a-input placeholder="请输入姓名" v-decorator="['name',  {rules: [{ required: true, message: '请输入姓名!' }]}]"/>
            </a-form-item>
          </a-col>


          <a-col :md="6" >
            <a-form-item
              label="身份证号">
                <a-input placeholder="请输入身份证号" v-decorator="['cardId', {}]"/>
            </a-form-item>
          </a-col>


          <a-col :md="6" :sm="24">
            <a-form-item
              label="性别">
              <!--<a-input placeholder="请输入性别" v-decorator="['sex', {}]" />-->
              <j-dict-select-tag style="width: 181px" v-decorator="['sex', {}]" :triggerChange="true"
                                 placeholder="请选择"
                                 dictCode="sex"/>
            </a-form-item>
          </a-col>

          <!--<a-col :md="6" :sm="24">-->
          <!--<a-form-item-->
          <!--label="批次">-->
          <!--&lt;!&ndash;<a-input placeholder="请输入批次" v-decorator="['batch', {}]" />&ndash;&gt;-->
          <!--<j-dict-select-tag style="width: 181px" v-decorator="['batch', {}]" :triggerChange="true" placeholder="请输入批次"-->
          <!--dictCode="xc_batch"/>-->
          <!--</a-form-item>-->
          <!--</a-col>-->

          <a-col :md="6" :sm="24">
            <a-form-item
              label="民族"
              hasFeedback>
              <!--<a-input placeholder="请输入民族" v-decorator="['nation', {}]"/>-->
              <j-dict-select-tag style="width: 181px" v-decorator="['nation', {}]" :triggerChange="true"
                                 placeholder="请选择"
                                 dictCode="xc_nation"/>
            </a-form-item>
          </a-col>

        </a-row>

        <a-row :gutter="24">


          <a-col :md="6" :sm="24">

            <a-form-item
              label="工作单位"
              hasFeedback>
              <a-input placeholder="请输入工作单位" v-decorator="['workAddress', {}]"/>
            </a-form-item>

          </a-col>

          <a-col :md="6" :sm="24">
            <a-form-item
              label="联系地址"
              hasFeedback>
              <a-input placeholder="请输入联系地址" v-decorator="['homeAddress', {}]"/>
            </a-form-item>
          </a-col>




          <a-col :md="6" :sm="24">
            <a-form-item
              label="所属校区">
              <!--<a-input placeholder="请输入所属校区" v-decorator="['newSchool', {}]"/>-->
              <!--<a-select style="width: 181px" :options="optionsSchool" placeholder="请选择校区" v-decorator="['newSchool', {}]">-->
              <!--</a-select>-->
              <j-dict-select-tag style="width: 181px" v-decorator="['newSchool', {}]" :triggerChange="true"
                                 placeholder="请输入"
                                 dictCode="xc_school"/>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="24">
            <a-form-item
              label="原学历">
              <!--<a-input placeholder="请输入原学历" v-decorator="['oldEdu', {}]" />-->
              <j-dict-select-tag style="width: 181px" v-decorator="['oldEdu', {}]" :triggerChange="true"
                                 placeholder="请输入"
                                 dictCode="xc_edu"/>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">

          <a-col :md="6" :sm="24">

            <a-form-item
              label="原毕业院校">
              <a-input placeholder="请输入原毕业院校" v-decorator="['oldSchool', {}]"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">

            <a-form-item
              label="原专业">
              <a-input placeholder="请输入原专业" v-decorator="['oldMajor', {}]"/>
            </a-form-item>
          </a-col>
          <!--<a-col :md="6" :sm="24">-->


          <!--<a-form-item-->
          <!--label="毕业时间">-->
          <!--<a-date-picker showTime format="YYYY-MM-DD HH:mm:ss" v-decorator="[ 'oldGradute', {}]"/>-->
          <!--</a-form-item>-->
          <!--</a-col>-->

          <a-col :md="6" :sm="24">

            <a-form-item
              label="政治面貌">
              <!--<a-input placeholder="请输入政治面貌" v-decorator="['politics', {}]" />-->
              <j-dict-select-tag style="width: 181px" v-decorator="['politics', {}]" :triggerChange="true"
                                 placeholder="请输入政治面貌"
                                 dictCode="xc_politics"/>
            </a-form-item>
          </a-col>



          <!--<a-col :md="6" :sm="24">-->
          <!--<a-form-item-->
          <!--label="学籍状态">-->
          <!--&lt;!&ndash;<a-input placeholder="请输入学籍状态" v-decorator="['eduStatus', {}]" />&ndash;&gt;-->
          <!--<j-dict-select-tag style="width: 181px" v-decorator="['eduStatus', {}]" :triggerChange="true" placeholder="请输入学籍状态"-->
          <!--dictCode="xc_edu_status"/>-->
          <!--</a-form-item>-->
          <!--</a-col>-->

          <a-col :md="6" :sm="24">
            <a-form-item
              label="邮箱">
              <!--<a-input placeholder="请输入邮箱" v-decorator="['email', {}]" />-->
              <a-auto-complete
                style="width: 181px"
                v-decorator="['email', {}]"
                :dataSource="dataSource"
                @change="handleChange"
                placeholder="Email"
              />
            </a-form-item>
          </a-col>

        </a-row>

        <a-row :gutter="24">


          <a-col :md="6" :sm="24">

            <a-form-item
              label="招生老师">
              <j-dict-select-tag style="width: 181px" v-decorator="['createName', {}]" :triggerChange="true"
                                 placeholder="请选择"
                                 dictCode="xc_teacher"/>
              <!--<a-input placeholder="请输入招生老师" v-decorator="['createName', {}]"/>-->
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="24">
            <a-form-item
              label="联系电话">
              <a-input placeholder="请输入联系电话" v-decorator="['phone', {}]"/>
            </a-form-item>
          </a-col>

          <!--<a-col :md="6" :sm="24">-->
            <!--<a-form-item-->
              <!--label="推荐人">-->
              <!--<a-input placeholder="请输入推荐人" v-decorator="['recommendPeople', {}]"/>-->
            <!--</a-form-item>-->
          <!--</a-col>-->

          <a-col :md="6" :sm="24">
            <a-form-item
              label="微信号">
              <a-input placeholder="请输入微信号" v-decorator="['wechat', {}]"/>
            </a-form-item>
          </a-col>



          <a-col :md="6" :sm="24" v-has="'show:所属平台'">
            <a-form-item
              label="所属平台">
              <!--<a-input placeholder="请输入政治面貌" v-decorator="['politics', {}]" />-->
              <j-dict-select-tag  style="width: 181px" v-decorator="['belong', {}]" :triggerChange="true"
                                 placeholder="请选择"
                                 dictCode="xc_platform"/>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item
              label="报名时间"
            >
              <a-date-picker style="width:164px" showTime format="YYYY-MM-DD HH:mm:ss" v-decorator="[ 'createTime', {}]" />
            </a-form-item>
          </a-col>

        </a-row>


        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item
              label="特殊情况">
              <a-textarea rows="5" style="width: 950px" placeholder="请输入" v-decorator="['remark', {}]"/>
            </a-form-item>
          </a-col>
        </a-row>


        <!--<a-form-item-->
        <!--:labelCol="labelCol"-->
        <!--:wrapperCol="wrapperCol"-->
        <!--label="更新人姓名">-->
        <!--<a-input placeholder="请输入更新人姓名" v-decorator="['updateName', {}]" />-->
        <!--</a-form-item>-->
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  import JDate from '@/components/jeecg/JDate'
  import pick from 'lodash.pick'
  import moment from 'moment'
  import JDictSelectTag from '@/components/dict/JDictSelectTag'
  import ATextarea from 'ant-design-vue/es/input/TextArea'
  import ACol from 'ant-design-vue/es/grid/Col'
  import ARow from 'ant-design-vue/es/grid/Row'

  export default {
    name: 'StudentModal',
    components: {
      ARow,
      ACol,
      ATextarea,
      JDate,
      JDictSelectTag
    },
    data() {
      return {
        optionsSchool: [],
        title: '操作',
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          add: '/student/student/add',
          edit: '/student/student/edit',
          schoolChooseList: '/student/student/querySchoolChooseByMainId',
          eduChangeList: '/student/student/queryEduChangeByMainId',
          examineList: '/student/student/queryExamineByMainId',
          platformList: '/student/student/queryPlatformByMainId'
        },
        dataSource: [],
      }
    },
    created() {
      this.initOptionsSchool()
    },
    watch: {
      'title': function(res) {
        console.log('res----->', res)
      }

    },
    methods: {
      handleChange(value) {
        this.dataSource = !value || value.indexOf('@') >= 0 ? [] : [
          `${value}@gmail.com`,
          `${value}@163.com`,
          `${value}@qq.com`
        ]
      },
      // 加载校区
      initOptionsSchool() {
        var url = '/school/school/listAll'

        getAction(url).then((res) => {
          if (res.success) {
            var result = res.result
            result.forEach(r => {
              this.optionsSchool.push({
                value: r.schoolName,
                label: r.schoolName
              })
            })
          }
        })
      },
      add() {

        this.edit({})
      },

      edit(record) {
        console.log("record------》",record)
        this.form.resetFields()
        this.model = Object.assign({}, record)
        //初始化明细表数据
        console.log(this.model.id)
        this.visible = true
        this.$nextTick(() => {
          // this.form.setFieldsValue(pick(this.model, 'name', 'sex', 'cardId', 'batch', 'newSchool', 'oldEdu', 'oldSchool', 'oldMajor', 'oldGradute', 'politics', 'createName', 'remark', 'eduStatus', 'email', 'phone', 'recommendPeople', 'wechat', 'updateName'))
          this.form.setFieldsValue(pick(this.model, 'eduStatus','belong', 'name', 'sex', 'cardId', 'homeAddress', 'workAddress', 'nation', 'newSchool', 'oldEdu', 'oldSchool', 'oldMajor', 'politics', 'createName', 'remark', 'email', 'phone', 'recommendPeople', 'wechat', 'updateName','createTime'))
          // 时间格式化
          this.form.setFieldsValue({ oldGradute: this.model.oldGradute ? moment(this.model.oldGradute) : null })
          this.form.setFieldsValue({ createTime: this.model.createTime ? moment(this.model.createTime) : null })


          // console.log("this.model.newSchool---->",this.model)
          this.form.setFieldsValue({ newSchool: this.model.newSchool })
        })

      },
      close() {
        this.$emit('close')
        this.visible = false
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)

            // console.log("this.model----->",this.model)
            // return;
            //时间格式化
            formData.oldGradute = formData.oldGradute ? formData.oldGradute.format('YYYY-MM-DD HH:mm:ss') : null
            formData.createTime = formData.createTime ? formData.createTime.format('YYYY-MM-DD HH:mm:ss') : null
            // 因为这边只有报名是新增
            if(!formData.eduStatus){
              formData.eduStatus = '1'
            }


            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                if (that.title === '报名') {
                  that.$message.success(that.title + '成功')
                } else {
                  that.$message.success(res.message)
                }
                that.$emit('ok')
              } else {
                if (that.title === '报名') {
                  that.$message.warning(that.title + '成功')
                } else {
                  that.$message.warning(res.message)
                }
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
      updateEduStatus(record, title) {
        let that = this
        let httpurl = this.url.edit
        httpAction(httpurl, record, 'put').then((res) => {
          if (res.success) {
            that.$message.success(title + '成功')
            that.$emit('ok')
          } else {
            that.$message.warning(res.message)
          }
        }).finally(() => {
          that.confirmLoading = false
        })
      }
    }
  }
</script>

<style scoped>
  .input-bottom {
  }

  .ant-btn {
    padding: 0 10px;
    margin-left: 3px;
  }

  .ant-form-item-control {
    line-height: 0px;
  }

  /** 主表单行间距 */
  .ant-form .ant-form-item {
    margin-bottom: 10px;
  }

  /** Tab页面行间距 */
  .ant-tabs-content .ant-form-item {
    margin-bottom: 0px;
  }
</style>